<script src="/static/plugins/bootstrap-table/bootstrap-table.js" type="text/javascript"></script>
<script src="/static/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js" type="text/javascript"></script>
<script src="/static/plugins/bootstrap-table/extensions/export/bootstrap-table-export.js"></script>
<script src="/static/plugins/bootstrap-table/extensions/export/tableExport.js"></script>
<script src="/static/plugins/FileSaver/FileSaver.js"></script>

<script>
    var $dataGrid1 = $('#dataGrid1');  //按客户
    var $dataGrid2 = $('#dataGrid2');  //按设备类型
    var $dataGrid3 = $('#dataGrid3');  //按通讯类型
    var $dataGrid4 = $('#dataGrid4');  //明细数据

    //初始化加载
    $(function () {
        pageInit();
    });

    //初始化页面
    function pageInit() {
        //初始化查询条件
        queryParamsInit();

        //初始化表格
        dataGridInit();

        //搜索
        $("#btnSearch").on("click", function (e) {
            search();
        });

        //保持搜索条件面板状态
        sdtheme.searchPanelStatusInit('searchpanelbtn');
    }

    //查询条件初始化
    function queryParamsInit() {
        //从cookie加载查询条件，传入参数为form id
        sdtheme.loadSearchText('searchForm');

        layui.use('laydate', function () {
            //当前范围 减 6
            var cur = new Date();
            var tmp_cur = new Date();
            var predate = new Date(tmp_cur.setDate(cur.getDate() - 6));
            var yesday = new Date(tmp_cur.setDate(cur.getDate() - 1));
            var result = predate.format('yyyy-MM-dd') + ' - ' + yesday.format('yyyy-MM-dd');

            var laydate = layui.laydate;
            laydate.render({
                elem: '#CollectDate',
                range: true,
                //yyyy-MM-dd - yyyy-MM-dd
                value: result,
                max: -1
            });
        });
    }

    function formatterText(data) {
        return "合计";
    }

    function formatterCount(data) {
        return data.length;
    }

    function formatterSum(data) {
        field = this.field;
        return data.reduce(function(sum, row) {
            return sum + (+row[field]);
        }, 0);
    }

    function formatterAvg(data) {
        return sumFormatter.call(this, data) / data.length;
    }

    //bootstrap table data init
    function dataGridInit() {
        //按客户
        $dataGrid1.bootstrapTable({
            showFooter: true,
            showRefresh: true,
            showExport: true,
            exportDataType: 'all',
            //exportTypes: ['csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf'],

            columns:[
                {
                    field: 'CollectDate',
                    title: '采集日期',
                    width: '120px',
                    align: 'center',
                    footerFormatter: formatterText
                }, {
                    field: 'CustomerNO',
                    title: '客户编号',
                    align: 'center',
                    width: '120px',
                    footerFormatter: formatterCount
                }, {
                    field: 'CustomerName',
                    title: '客户名称',
                    width: '110px',
                    align: 'center'
                }, {
                    field: 'NeedRows',
                    title: '应采条数',
                    width: '110px',
                    align: 'center',
                    footerFormatter: formatterSum
                }, {
                    field: 'Rows',
                    title: '实采条数',
                    width: '110px',
                    align: 'center',
                    footerFormatter: formatterSum
                }, {
                    field: 'Rate',
                    title: '实采比率',
                    width: '110px',
                    align: 'center',
                    formatter: sdtheme.showTableRate
                }
            ]
        });

        //按设备类型
        $dataGrid2.bootstrapTable({
            showFooter: true,
            showRefresh: true,
            showExport: true,
            exportDataType: 'all',

            columns:[
                {
                    field: 'CollectDate',
                    title: '采集日期',
                    width: '120px',
                    align: 'center',
                    footerFormatter: formatterText
                }, {
                    field: 'MeterTypeNO',
                    title: '设备类型编号',
                    align: 'center',
                    width: '120px',
                    footerFormatter: formatterCount
                }, {
                    field: 'MeterType',
                    title: '设备类型',
                    width: '110px',
                    align: 'center'
                }, {
                    field: 'NeedRows',
                    title: '应采条数',
                    width: '110px',
                    align: 'center',
                    footerFormatter: formatterSum
                }, {
                    field: 'Rows',
                    title: '实采条数',
                    width: '110px',
                    align: 'center',
                    footerFormatter: formatterSum
                }, {
                    field: 'Rate',
                    title: '实采比率',
                    width: '110px',
                    align: 'center',
                    formatter: sdtheme.showTableRate
                }
            ]
        });

        //按通讯类型
        $dataGrid3.bootstrapTable({
            showFooter: true,
            showRefresh: true,
            showExport: true,
            exportDataType: 'all',

            columns:[
                {
                    field: 'CollectDate',
                    title: '采集日期',
                    width: '120px',
                    align: 'center',
                    footerFormatter: formatterText
                }, {
                    field: 'GatewayNO',
                    title: '通讯类型编号',
                    align: 'center',
                    width: '120px',
                    footerFormatter: formatterCount
                }, {
                    field: 'GatewayDesc',
                    title: '通讯类型',
                    width: '110px',
                    align: 'center'
                }, {
                    field: 'NeedRows',
                    title: '应采条数',
                    width: '110px',
                    align: 'center',
                    footerFormatter: formatterSum
                }, {
                    field: 'Rows',
                    title: '实采条数',
                    width: '110px',
                    align: 'center',
                    footerFormatter: formatterSum
                }, {
                    field: 'Rate',
                    title: '实采比率',
                    width: '110px',
                    align: 'center',
                    formatter: sdtheme.showTableRate
                }
            ]
        });

        //明细数据
        $dataGrid4.bootstrapTable({
            showFooter: true,
            cache: true,
            striped: true,    //是否使用缓存
            pageSize: 10,
            pageList: [10, 20, 50, 100, 200],
            sidePagination: 'client',
            pagination: true,      //在表格底部显示分页工具栏

            showRefresh: true,
            showExport: true,
            exportDataType: 'all',

            columns: [
                {
                    field: 'CollectDate',
                    title: '采集日期',
                    width: '160px',
                    align: 'center',
                    formatter: sdtheme.formatterDateBySpan,
                    footerFormatter: formatterText
                }, {
                    field: 'CustomerNO',
                    title: '客户编号',
                    align: 'center',
                    width: '120px',
                    footerFormatter: formatterCount
                }, {
                    field: 'CustomerName',
                    title: '客户名称',
                    width: '110px',
                    align: 'center'
                }, {
                    field: 'DTU_no',
                    title: 'DTU编号',
                    align: 'center',
                    width: '120px'
                }, {
                    field: 'MeterAddress',
                    title: '表地址',
                    width: '90px',
                    align: 'center'
                }, {
                    field: 'MeterTypeNO',
                    title: '设备类型编号',
                    align: 'center',
                    width: '120px'
                }, {
                    field: 'MeterType',
                    title: '设备类型',
                    width: '110px',
                    align: 'center'
                }, {
                    field: 'GatewayNO',
                    title: '通讯类型编号',
                    align: 'center',
                    width: '120px'
                }, {
                    field: 'GatewayDesc',
                    title: '通讯类型',
                    width: '110px',
                    align: 'center'
                }, {
                    field: 'NeedRows',
                    title: '应采条数',
                    width: '110px',
                    align: 'center',
                    footerFormatter: formatterSum
                }, {
                    field: 'Rows',
                    title: '实采条数',
                    width: '110px',
                    align: 'center',
                    footerFormatter: formatterSum
                }, {
                    field: 'Rate',
                    title: '实采比率',
                    width: '110px',
                    align: 'center',
                    formatter: sdtheme.showTableRate
                }
            ]
        });
    }

    function compare(property) {
        return function (a, b) {
            var v1 = a[property];
            var v2 = b[property];
            return v1 - v2;
        }
    }

    function search() {
        var url = '{{ urlfor "EquipmentOverviewController.DataList"}}';
        var params = {
            CollectDate: $('.searchForm input[name="CollectDate"]').val()
        };

        $.sdpost(url, params, function (re) {
            if (re.code === 0) {
                var len = Object.keys(re.obj).length - 1;

                fillGrid1(re.obj, len);
                fillGrid2(re.obj, len);
                fillGrid3(re.obj, len);
                fillGrid4(re.obj, len);
            }
        });
    }

    function fillGrid1(obj, len){
        var html = [];
        var old_temp = null;

        var NeedRows = 0;
        var Rows = 0;
        var beginTime = null;
        var endTime = null;

        obj.map(function (item, index, array) {
            var CustomerNO = item.CustomerNO;

            if( ((old_temp!== null) && (old_temp !== CustomerNO)) || (index === len)){
                if(index === len){
                    NeedRows = NeedRows + item.NeedRows;
                    Rows = Rows + item.Rows;
                }

                endTime = array[index - 1].CollectDate;
                var jsondata = {CollectDate: sdtheme.formatDate(beginTime) + '~' + sdtheme.formatDate(endTime),
                                CustomerNO: array[index - 1].CustomerNO,
                                CustomerName: array[index - 1].CustomerName,
                                NeedRows: NeedRows,
                                Rows: Rows,
                                Rate: (Rows / NeedRows * 100).toFixed(2)
                               };
                html.push(jsondata);

                NeedRows = 0;
                Rows = 0;
                Num = 0;
                beginTime = null;
                endTime = null;
                old_temp = null;
            }

            if(beginTime === null){
                beginTime = item.CollectDate;
            }

            NeedRows = NeedRows + item.NeedRows;
            Rows = Rows + item.Rows;
            old_temp = CustomerNO;
        });

        $dataGrid1.bootstrapTable('load', html);
    }

    function fillGrid2(obj, len){
        var sortObj = obj.sort(compare('MeterTypeNO'));

        var html = [];
        var old_temp = null;

        var NeedRows = 0;
        var Rows = 0;
        var beginTime = null;
        var endTime = null;

        sortObj.map(function (item, index, array) {
            var MeterTypeNO = item.MeterTypeNO;
            var item_now = new Date(item.CollectDate);

            if(beginTime === null){
                beginTime = item_now;
                endTime = item_now;
            }

            if( ((old_temp!== null) && (old_temp !== MeterTypeNO)) || (index === len)){
                if(index === len){
                    NeedRows = NeedRows + item.NeedRows;
                    Rows = Rows + item.Rows;

                    if(beginTime > item_now){
                        beginTime = item_now;
                    }

                    if(endTime < item_now){
                        endTime = item_now;
                    }
                }

                var jsondata = {CollectDate: sdtheme.formatDate(beginTime) + '~' + sdtheme.formatDate(endTime),
                    MeterTypeNO: array[index - 1].MeterTypeNO,
                    MeterType: array[index - 1].MeterType,
                    NeedRows: NeedRows,
                    Rows: Rows,
                    Rate: (Rows / NeedRows * 100).toFixed(2)
                };
                html.push(jsondata);

                NeedRows = 0;
                Rows = 0;
                beginTime = null;
                endTime = null;
                old_temp = null;
            }

            if(beginTime > item_now){
                beginTime = item_now;
            }

            if(endTime < item_now){
                endTime = item_now;
            }

            NeedRows = NeedRows + item.NeedRows;
            Rows = Rows + item.Rows;
            old_temp = MeterTypeNO;
        });

        $dataGrid2.bootstrapTable('load', html);
    }

    function fillGrid3(obj, len){
        var sortObj = obj.sort(compare('GatewayNO'));

        var html = [];
        var old_temp = null;

        var NeedRows = 0;
        var Rows = 0;
        var beginTime = null;
        var endTime = null;

        sortObj.map(function (item, index, array) {
            var GatewayNO = item.GatewayNO;
            var item_now = new Date(item.CollectDate);

            if(beginTime === null){
                beginTime = item_now;
                endTime = item_now;
            }

            if( ((old_temp!== null) && (old_temp !== GatewayNO)) || (index === len)){
                if(index === len){
                    NeedRows = NeedRows + item.NeedRows;
                    Rows = Rows + item.Rows;

                    if(beginTime > item_now){
                        beginTime = item_now;
                    }
                    if(endTime < item_now){
                        endTime = item_now;
                    }
                }

                var jsondata = {CollectDate: sdtheme.formatDate(beginTime) + '~' + sdtheme.formatDate(endTime),
                    GatewayNO: array[index - 1].GatewayNO,
                    GatewayDesc: array[index - 1].GatewayDesc,
                    NeedRows: NeedRows,
                    Rows: Rows,
                    Rate: (Rows / NeedRows * 100).toFixed(2)
                };
                html.push(jsondata);

                NeedRows = 0;
                Rows = 0;
                beginTime = null;
                endTime = null;
                old_temp = null;
            }

            if(beginTime > item_now){
                beginTime = item_now;
            }

            if(endTime < item_now){
                endTime = item_now;
            }

            NeedRows = NeedRows + item.NeedRows;
            Rows = Rows + item.Rows;
            old_temp = GatewayNO;
        });

        $dataGrid3.bootstrapTable('load', html);
    }

    function fillGrid4(obj, len){
        var sortObj = obj.sort(function (a, b) {
            if(a['CustomerNO'] === b['CustomerNO']){
                if(a['CollectDate'] === b['CollectDate']){
                    return 0;
                }else if(a['CollectDate'] > b['CollectDate'])
                    return 1;
                else
                    return -1;
            }else if(a['CustomerNO'] > b['CustomerNO']){
                return 1;
            }else{
                return -1;
            }
        });

        $dataGrid4.bootstrapTable('load', sortObj);
    }

</script>